package com.sf.algo.shortpath;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class MapUtil {
	/**
	 * 传障碍物获得有障碍物的12*12 的map
	 * @param barlist
	 * @return
	 */
	public static int[][] getBarMap(List<Coord> barlist) {
		int [][] barMap = new int[12][12];
		for (int i = 0; i < barMap.length; i++)
		{
			for (int j = 0; j < barMap[i].length; j++)
			{
				barMap[j][i] = 0;
			}
		}
		
		if (barlist != null && !barlist.isEmpty()) {
			for(Coord coord : barlist) {
				barMap[coord.getX()][coord.getY()] = 1;
			}
		}
		//printMap(barMap);
		return barMap;
	}
	/**
	 */
	public static  int[][] getBarMap() {
		
		return getBarMap(getBar());
	}
	public static List<Coord> getBar() {
		String jsonStr = "[{" +
				"                \"y\": 2," + 
				"                \"x\": 1" + 
				"            }," + 
				"            {" + 
				"                \"y\": 3," + 
				"                \"x\": 2" + 
				"            }," + 
				"            {" + 
				"                \"y\": 3," + 
				"                \"x\": 4" + 
				"            }," + 
				"            {" + 
				"                \"y\": 4," + 
				"                \"x\": 4" + 
				"            }," + 
				"            {" + 
				"                \"y\": 6," + 
				"                \"x\": 9" + 
				"            }," + 
				"            {" + 
				"                \"y\": 6," + 
				"                \"x\": 8" + 
				"            }," + 
				"            {" + 
				"                \"y\": 5," + 
				"                \"x\": 6" + 
				"            }," + 
				"            {" + 
				"                \"y\": 8," + 
				"                \"x\": 6" + 
				"            }," + 
				"            {" + 
				"                \"y\": 9," + 
				"                \"x\": 6" + 
				"            }," + 
				"            {" + 
				"                \"y\": 9," + 
				"                \"x\": 3" + 
				"            }," + 
				"            {" + 
				"                \"y\": 9," + 
				"                \"x\": 2" + 
				"            }," + 
				"            {" + 
				"                \"y\": 10," + 
				"                \"x\": 2" + 
				"            }," + 
				"            {" + 
				"                \"y\": 2," + 
				"                \"x\": 9" + 
				"            }," + 
				"            {" + 
				"                \"y\": 2," + 
				"                \"x\": 8" + 
				"            }]";
			JSONArray bars =  JSON.parseArray(jsonStr);
			List<Coord> barlist = new ArrayList<>();
			for(int i = 0;i<bars.size(); i++) {
				JSONObject bar = bars.getJSONObject(i);
				barlist.add(new Coord(bar.getIntValue("x"),bar.getIntValue("y")));
			}
			return barlist;
	}
	public static void printMap(int [][] maps) {
		System.out.println("-------------------------------");
		for (int i = 0; i < maps.length; i++)
		{
			for (int j = 0; j < maps[i].length; j++)
			{
				System.out.print(maps[i][j] + " ");
			}
			System.out.println();
		}
	}
}
